package cn.whale.mcp;

import cn.whale.assistant.McpAssistant;
import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.mcp.McpToolProvider;
import dev.langchain4j.mcp.client.DefaultMcpClient;
import dev.langchain4j.mcp.client.McpClient;
import dev.langchain4j.mcp.client.transport.McpTransport;
import dev.langchain4j.mcp.client.transport.stdio.StdioMcpTransport;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.tool.ToolProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;

@Configuration
public class McpConfig {


    @Bean
    public McpAssistant mcpAssistant(QwenChatModel qwenChatModel){

        try{
            McpTransport transport = new StdioMcpTransport.Builder()
                    .command(List.of("cmd",  "/c","npx","-y","@amap/amap-maps-mcp-server"))
                    .environment(Map.of("AMAP_MAPS_API_KEY","faeeb02bc17355549996e51933d2c045"))
                    .logEvents(true)
                    .build();

            McpClient mcpClient = new DefaultMcpClient.Builder()
                    .transport(transport)
                    .build();

            ToolProvider toolProvider = McpToolProvider.builder()
                    .mcpClients(List.of(mcpClient))
                    .build();

            return AiServices.builder(McpAssistant.class)
                    .chatLanguageModel(qwenChatModel)
                    .toolProvider(toolProvider).build();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
